

package flc.ast.adapter;


import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.FileUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.io.File;
import java.util.List;

import flc.ast.R;
import flc.ast.bean.AlbumBean;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;

public class AlbumAdapter extends StkProviderMultiAdapter<AlbumBean> {

    public AlbumAdapter() {
        addItemProvider(new StkSingleSpanProvider(126));
        addItemProvider(new AlbumProvider());
    }

    private boolean isEdit;

    public void setEdit(boolean edit) {
        isEdit = edit;
        notifyDataSetChanged();
    }

    private class AlbumProvider extends BaseItemProvider<AlbumBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_album;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, AlbumBean bean) {
            baseViewHolder.setText(R.id.tvAlbumItemName, FileUtils.getFileName(bean.getPath()));
            List<File> files = FileUtils.listFilesInDir(bean.getPath());
            if (files.size() != 0) {
                Glide.with(baseViewHolder.itemView).load(files.get(0).getPath()).into((ImageView) baseViewHolder.getView(R.id.ivAlbumItemImg));
            } else {
                baseViewHolder.setImageResource(R.id.ivAlbumItemImg, R.drawable.azwzp);
//                baseViewHolder.setText(R.id.tvAlbumItemName, getContext().getString(R.string.add_picture));
            }
            if (isEdit) {
                baseViewHolder.getView(R.id.ivAlbumItemSel).setVisibility(View.VISIBLE);
                if (bean.isSelected()) {
                    baseViewHolder.setImageResource(R.id.ivAlbumItemSel, R.drawable.axcxz);
                } else {
                    baseViewHolder.setImageResource(R.id.ivAlbumItemSel, R.drawable.axcwxz);
                }
            } else {
                baseViewHolder.getView(R.id.ivAlbumItemSel).setVisibility(View.GONE);
            }
            baseViewHolder.setText(R.id.tvAlbumItemSize, getContext().getString(R.string.project_num, files.size()));
        }
    }

}
